/**	
 * <br>
 * Copyright 2011 Chuan. All rights reserved.<br>
 * <br>			 
 * Package: com.chuan.mantu.utils <br>
 * FileName: ViewUtil.java <br>
 * <br>
 * @version
 * @author javacyc@qq.com
 * @created 2012-11-16
 * @last Modified 
 * @history
 */

package com.chuan.mantu.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.chuan.mantu.vo.Pic;

/**
 * {View工具类}
 *  
 *  @author javacyc@qq.com
 *  @created 2012-11-16 下午9:25:28
 *  @lastModified       
 *  @history           
 */

public class ViewUtil {
	
	/**
	 * 
	 *  ｛添加文件夹下的本地资源图片文件｝
	 *  @param context Context
	 *  @param imagePathName 图片绝对路径名称
	 *  @return
	 *  @author javacyc@qq.com
	 *  @created 2012-11-15 下午11:37:24
	 *  @exception OutOfMemoryError
	 *  @lastModified
	 *  @history
	 */
	public static View addImageView(Context context,String imagePathName) throws OutOfMemoryError{
		BitmapFactory.Options opt = new BitmapFactory.Options();
	    opt.inPreferredConfig = Bitmap.Config.RGB_565;
	    opt.inPurgeable = true;
	    opt.inInputShareable = true;
		SoftReference<ImageView> srf = new SoftReference<ImageView>(new ImageView(context));
		FileInputStream fis = null;
        try {
        	fis = new FileInputStream(new File(imagePathName));
        	srf.get().setImageBitmap(BitmapFactory.decodeStream(fis,null,opt));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally{
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return srf.get();
	}
	
	/**
	 * 
	 *  ｛添加文件夹下的本地资源图片文件，刚出现OOM时通过缩放图片来避免｝
	 *  @param context
	 *  @param imagePathName 图片绝对路径名称
	 *  @return
	 *  @author javacyc@qq.com
	 *  @created 2013-3-1 下午03:29:20
	 *  @exception OutOfMemoryError
	 *  @lastModified       
	 *  @history
	 */
	public static View addImageViewShrink(Context context,String imagePathName) throws OutOfMemoryError{
		SoftReference<ImageView> srf = new SoftReference<ImageView>(new ImageView(context));
		BitmapFactory.Options opts =  new  BitmapFactory.Options();
        opts.inJustDecodeBounds =  true ;
        BitmapFactory.decodeFile(imagePathName, opts);
        opts.inSampleSize = ImageUtil.computeSampleSize(opts, 480 ,  480 * 320);       
        opts.inJustDecodeBounds =  false ;
        opts.inDither = false;
        opts.inPreferredConfig = Bitmap.Config.RGB_565;
		FileInputStream fis = null;
        try {
        	fis = new FileInputStream(new File(imagePathName));
        	srf.get().setImageBitmap(BitmapFactory.decodeStream(fis,null,opts));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally{
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return srf.get();
	}
	
	public static Bitmap addBitmapShrink(String imagePathName) throws OutOfMemoryError{
		BitmapFactory.Options opts =  new  BitmapFactory.Options();
        opts.inJustDecodeBounds =  true ;
        BitmapFactory.decodeFile(imagePathName, opts);
        opts.inSampleSize = ImageUtil.computeSampleSize(opts, -1 ,  128 * 128);       
        opts.inJustDecodeBounds =  false ;
        opts.inDither = false;
        opts.inPreferredConfig = Bitmap.Config.RGB_565;
		FileInputStream fis = null;
		Bitmap bitmap = null;
        try {
        	fis = new FileInputStream(new File(imagePathName));
        	bitmap = BitmapFactory.decodeStream(fis,null,opts);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally{
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return bitmap;
	}
	
	/**
	 * 
	 *  ｛添加指定ID资源｝
	 *  @param context Context
	 *  @param imageId 资源ID
	 *  @return
	 *  @author javacyc@qq.com
	 *  @created 2012-11-20 下午11:07:51
	 *  @lastModified       
	 *  @history
	 */
	public static View addImageView(Context context,int imageId) {
		ImageView iv = new ImageView(context);
		iv.setImageResource(imageId);
		return iv;
	}
	
	/**
	 * 
	 *  ｛向图片展现列表中填充图片<br>｝
	 *  @param lv 需要填充的展现列表
	 *  @param list Pic列表
	 *  @author javacyc@qq.com
	 *  @created 2012-11-20 上午10:22:23
	 *  @lastModified       
	 *  @history
	 */
	public static void fillListImageView(Context context, String category , List<View> lv,ArrayList<Pic> list){
		if(list != null && !list.isEmpty()){
			String rootPath =	Environment.getExternalStorageDirectory().getAbsolutePath()
			+ Const.PATH_BACKLASH 
			+ Const.SDCARD_FILENAME
			+ Const.PATH_BACKLASH 
			+ Const.PIC_ROOT_PATH 
			+ Const.PATH_BACKLASH 
			+ category 
			+ Const.PATH_BACKLASH;
			for(Pic pic : list){
				try {
					lv.add(addImageView(context,rootPath + pic.getName()));
				} catch (OutOfMemoryError e) {
					System.gc();
					Log.d(Const.DEBUG, rootPath + pic.getName() + " OOM Exception , Shrink this picture");
					try {
						lv.add(addImageViewShrink(context,rootPath + pic.getName()));
					} catch (OutOfMemoryError e1) {
						Log.d(Const.DEBUG, rootPath + pic.getName() + " still OOM Exception , Pass this picture");
						continue;
					}
				}
			}
		}
	}
	
	/**
	 * 
	 *  ｛返回本地资源的Bitmap实例｝
	 *  @param context Context
	 *  @param imagePathName 文件路径
	 *  @return Bitmap
	 *  @author javacyc@qq.com
	 *  @created 2012-11-22 上午09:23:35
	 *  @lastModified       
	 *  @history
	 */
	public static Bitmap getImageBitmap(Context context,String imagePathName){
		String rootPath = Environment.getExternalStorageDirectory().getAbsolutePath()
						+ Const.PATH_BACKLASH 
						+ Const.SDCARD_FILENAME
						+ Const.PATH_BACKLASH 
						+ Const.PIC_ROOT_PATH 
						+ Const.PATH_BACKLASH; 
			try {
				return  BitmapFactory.decodeStream(new FileInputStream(new File(rootPath + imagePathName)));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			return null;
	}
	
}